# -*- encoding:utf-8 -*-
import time
import datetime
from UI1 import Ui_MainWindow
import module
from PyQt5.QtCore import QThread, pyqtSignal,QMutex

class Thread_homepage(QThread):
    value = pyqtSignal(str)
    def __init__(self):
        super(Thread_homepage,self).__init__()
        self.mutex = QMutex()
        self.url1 = 'https://www.baidu.com'
        self.url2 = 'http://172.16.2.100'
        self.is_running = True

    def stop(self):
        self.is_running = False
        self.wait()

    #运行（入口）
    def run(self):
        while self.is_running:
            # 线程锁ON
            self.mutex.lock()
            account = module.account.obtainini(self)
            if account == '配置信息不存在':
                self.value.emit(account)
            else:
                if not module.verify(url=self.url1):
                    if module.verify(url=self.url2):
                        result = module.run(username=account[0], password=account[1]).requests()
                        self.value.emit(result)
                    else:
                        self.value.emit('未在校园网范围内')
                else:
                    self.value.emit('已连接互联网')
            self.value.emit(str(datetime.datetime.now()))
            # 线程锁off
            self.mutex.unlock()


class Thread_settingpage(QThread):
    value1 = pyqtSignal(str)

    def __init__(self):
        super(Thread_settingpage, self).__init__()
        self.mutex = QMutex()
        self.url1 = 'https://www.baidu.com'
        self.url2 = 'http://172.16.2.100'

    def run(self):
        self.value1.emit('启动自动断连')
        self.value1.emit(str(datetime.datetime.now()))
        account1 = module.account.obtainini(self)
        while not account1 == '配置信息不存在':
            while (True):
                # 线程锁ON
                self.mutex.lock()
                if not module.verify(self.url1):  # 判断是否联网
                    if module.verify(self.url2):  # 判断是否在校园网范围内
                        result = module.run(username=account1[0], password=account1[1]).requests()
                        self.value1.emit(result)
                        self.value1.emit(str(datetime.datetime.now()))
                    # 线程锁off
                self.mutex.unlock()
                time.sleep(5)

class my_mainwindow(Ui_MainWindow):
    def __init__(self,mainWindow):
        super().__init__()
        self.setupUi(mainWindow)
        self.initUi()
        self.url1 = 'https://www.baidu.com'
        self.url2 = 'http"//172.16.2.100'

    def initUi(self):
        self.work1 = Thread_homepage()
        self.work2 = Thread_settingpage()
        self.run_pushButton.clicked.connect(self.run_btn)
        self.exit_pushButton.clicked.connect(self.exit_btn)
        self.textBrowser
        self.save_pushButton.clicked.connect(self.save_btn)
        self.clean_pushButton.clicked.connect(self.clean_btn)
        self.break_pushButton.clicked.connect(self.break_btn)
        self.textBrowser_2
        self.Reconnection_checkBox.clicked.connect(self.reconnection)
        self.work2 = Thread_settingpage()
        self.work2.value1.connect(self.textBrowser.append)
        self.work2.start()
        self.powerup_checkBox.stateChanged.connect(self.powerup)

    def run_btn(self):
        self.work1 = Thread_homepage()
        self.work1.value.connect(self.showbrw1)
        self.work1.start()

    def showbrw1(self,msg):
        self.textBrowser.append(msg)
        self.work1.stop()

    def exit_btn(self):
        exit()

    def save_btn(self):
        usename = self.account_lineEdit_2.text()
        password = self.password_lineEdit_2.text()
        module.account(usename, password).setini()
        self.textBrowser_2.append(usename)
        self.textBrowser_2.append(password)

    def clean_btn(self):
        module.account.cleanini(self)
        self.textBrowser_2.clear()

    def break_btn(self):
        self.textBrowser_2.clear()
        account = module.account.obtainini(self)
        username = account[0]
        password = account[1]
        self.textBrowser_2.append(username)
        self.textBrowser_2.append(password)

    def reconnection(self):
        if self.Reconnection_checkBox.isChecked():
            self.work2 = Thread_settingpage()
            self.work2.value1.connect(self.textBrowser.append)
            self.work2.start()
        else:
            self.work2.terminate()
            self.textBrowser.append('结束自动断连')
            self.textBrowser.append(str(datetime.datetime.now()))

    def powerup(self):
        pass